package activity.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.baidu.mobstat.StatService;
import com.ilioili.proabslist.ListStateListener;
import com.ilioili.proabslist.ProAbsListView;
import com.laoxinwen.app.R;
import com.umeng.analytics.MobclickAgent;

import cn.evun.proabslistview.FreshView;
import cn.evun.proabslistview.LoadView;
import common.BaseFragment;
import common.Constants;
import common.FastAdapter;

/**
 * Created by sshong on 15/6/17.
 */
public abstract class BaseFrgList extends BaseFragment implements ListStateListener {

    protected int lastFreshed;
    protected int pageIndex;

    protected ListView listView;
    protected ProAbsListView proAbsListView;

    protected FastAdapter listAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        proAbsListView = (ProAbsListView) inflater.inflate(R.layout.fragment_main_item_list, container, false);
        proAbsListView.setListStateListener(this);
//        cardPopupWindow = new CardPopupWindow(getActivity(), imgPool);
        setListView();
        setFreshView();
        setLoadView();
        if (savedInstanceState != null) {
//            lastFreshed = savedInstanceState.getInt("lastFresh");
        }
        return proAbsListView;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //上次刷新时间
        outState.putInt("lastFresh", lastFreshed);
    }

    /**
     * 用于友盟统计
     * @return
     */
    protected String pageName() {
        return "list";
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(pageName());
        StatService.onPageStart(getActivity(), pageName());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(pageName());
        StatService.onPageEnd(getActivity(), pageName());
    }

    private void setListView() {
        listView = (ListView) proAbsListView.getAbsListView();
        listView.setAdapter(listAdapter);
    }

    private void setLoadView() {
        LoadView loadView = new LoadView(getActivity());
        proAbsListView.setLoadView(loadView);
        if(bundle != null) {
            proAbsListView.setLoadable(!bundle.getBoolean("onepage"));
        }
    }

    private void setFreshView() {
        FreshView freshView = new FreshView(getActivity());
        proAbsListView.setFreshView(freshView);
    }

    @Override
    public void setBundle(Bundle bundle) {
        super.setBundle(bundle);
        if(proAbsListView != null) {
            proAbsListView.setLoadable(!bundle.getBoolean("onepage"));
        }
    }

    @Override
    public void onScrollWhileFreshViewVisible(int percentage) {

    }

    @Override
    public void onScrollWhileLoadViewVisible(int percentage) {

    }

    @Override
    public void onStartFreshing() {
        pageIndex = 0;
        requestData(true);
    }

    @Override
    public void onStartLoading() {
        pageIndex++;
        requestData(false);
    }

    /**
     * 检查是否需要刷新
     */
    public void checkFresh() {
        if (lastFreshed == 0 || (getCurrenTimeFlag() - lastFreshed) > Constants.AUTO_FRESH_FLAG) {
            proAbsListView.startFresh();
        }
    }

    protected int getCurrenTimeFlag() {
        return (int) (System.nanoTime() / 1000000000);
    }

    protected abstract void requestData(final boolean isFresh);
}
